package com.bnq.control.desktop.popup;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.bnq.control.R;
import com.oray.sunlogin.manager.DesktopPluginManager;
import com.oray.sunlogin.manager.util.ThreadUtils;

/**
 * Created by qianwei on 2019/3/19.
 */

public class MouseMenuPopup extends PopupWindow implements View.OnClickListener {

    private DesktopPluginManager mDesktopManager;
    private Context mContext;

    public MouseMenuPopup(Context context, DesktopPluginManager manager) {
        super(context);
        mContext = context;
        mDesktopManager = manager;
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setClippingEnabled(false);
        setOutsideTouchable(false);
        setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent));
        initView();
    }

    private void initView() {
        View contentView = View.inflate(mContext, R.layout.mouse_menu, null);
        contentView.findViewById(R.id.ll_mouse_menu_copy).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_paste).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_delete).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_cut).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_check_all).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_revoke).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_return_desktop).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_switch_window).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_close_window).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_start_menu).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_run).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_lock_system).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_explorer).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_task_manager).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_ctrl_alt_del).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_save).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_screenshots).setOnClickListener(this);
        contentView.findViewById(R.id.ll_mouse_menu_search).setOnClickListener(this);

        setContentView(contentView);
    }

    public void show(View token) {
        showAtLocation(token, Gravity.CENTER, 0, 0);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_mouse_menu_copy) {
            sendHotKeyMessage("c", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_paste) {
            sendHotKeyMessage("v", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_delete) {
            sendKeyUpDown("delete", 0, true);
            sendKeyUpDown("delete", 0, false);
        } else if (id == R.id.ll_mouse_menu_cut) {
            sendHotKeyMessage("x", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_check_all) {
            sendHotKeyMessage("a", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_revoke) {
            sendHotKeyMessage("z", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_return_desktop) {
            sendHotKeyMessage("d", "LWIN");
        } else if (id == R.id.ll_mouse_menu_switch_window) {
            sendHotKeyMessage("tab", "LWIN");
        } else if (id == R.id.ll_mouse_menu_close_window) {
            sendHotKeyMessage("F4", "LALT");
        } else if (id == R.id.ll_mouse_menu_start_menu) {
            sendKeyUpDown("LWIN", 0, true);
            sendKeyUpDown("LWIN", 0, false);
        } else if (id == R.id.ll_mouse_menu_run) {
            sendHotKeyMessage("r", "LWIN");
        } else if (id == R.id.ll_mouse_menu_lock_system) {
            sendHotKeyMessage("l", "LWIN");
        } else if (id == R.id.ll_mouse_menu_explorer) {
            sendHotKeyMessage("e", "LWIN");
        } else if (id == R.id.ll_mouse_menu_task_manager) {
            sendKeyUpDown("LCONTROL", 0, true);
            sendKeyUpDown("LSHIFT", 0, true);
            sendKeyUpDown("ESCAPE", 0, true);
            ThreadUtils.sleep(50);
            sendKeyUpDown("ESCAPE", 0, false);
            sendKeyUpDown("LSHIFT", 0, false);
            sendKeyUpDown("LCONTROL", 0, false);
        } else if (id == R.id.ll_mouse_menu_ctrl_alt_del) {
            sendKeyUpDown("LCONTROL", 0, true);
            sendKeyUpDown("LALT", 0, true);
            sendKeyUpDown("DELETE", 0, true);
            ThreadUtils.sleep(50);
            sendKeyUpDown("DELETE", 0, false);
            sendKeyUpDown("LALT", 0, false);
            sendKeyUpDown("LCONTROL", 0, false);
        } else if (id == R.id.ll_mouse_menu_save) {
            sendHotKeyMessage("s", "LCONTROL");
        } else if (id == R.id.ll_mouse_menu_screenshots) {
            sendHotKeyMessage("SNAPSHOT", "LALT");
        } else if (id == R.id.ll_mouse_menu_search) {
            sendHotKeyMessage("q", "LWIN");
        }
        dismiss();
    }

    private void sendKeyUpDown(String key, int metaState, boolean isDown) {
        mDesktopManager.sendKeyUpDown(key, metaState, isDown);
    }

    private void sendHotKeyMessage(String key, String metaState) {
        if (null != mDesktopManager) mDesktopManager.sendHotKeyMessage(key, metaState);
    }

}
